<?php
/**
 * Odstavce Model for Ohlasky Component
 *
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * Odstavce Ohlasky Model
 */
class OhlaskysModelOdstavce extends JModel
{
    /**
     * Constructor that retrieves the ID from the request
     *
     * @access	public
     * @return	void
     */
    function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);

    }

    /**
     * Method to set the odstavce identifier
     *
     * @access	public
     * @param	int odstavce identifier
     * @return	void
     */
    function setId($id)
    {
        // Set id and wipe data
        $this->_id		= $id;
        $this->_data	= null;
    }

    function getTyden_id_form() {
        $tyden_id = JRequest::getVar('tyden_id',  '', '', 'integer');
        return $tyden_id;
    }

    /**
     * Method to get a odstavce
     * @return object with data
     */
    function &getData()
    {
        // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM #__odstavce '.
                    '  WHERE id = '.$this->_id;
            $this->_db->setQuery( $query );
            $this->_data = $this->_db->loadObject();
        }
        if (!$this->_data) {
            //zjistime nejnizsi volne poradove cislo odstavce
            $tyden_id_int = $this->getTyden_id_form();
            $nove_por_cis = null;
            if (!empty($tyden_id_int)) {
                $query2 = 'SELECT * FROM #__odstavce'.
                    '  WHERE #__odstavce.tyden_id = '.$tyden_id_int.'
                        ORDER BY por_cis DESC';
                $this->_db->setQuery( $query2 );
                $predchozi_odst_obj = $this->_db->loadObject();
                if (is_object($predchozi_odst_obj)) {
                    $nove_por_cis = $predchozi_odst_obj->por_cis + 1;
                } else {
                    $nove_por_cis = 1;
                }
                
                $this->_data = new stdClass();
                $this->_data->id = 0;
                $this->_data->tyden_id = $this->getTyden_id_form();
                $this->_data->por_cis = $nove_por_cis;
                $this->_data->nadpis = null;
                $this->_data->obsah = null;
            }
        }
        return $this->_data;
    }

    /**
     * Method to store a record
     *
     * @access	public
     * @return	boolean	True on success
     */
    function store()
    {
        $row =& $this->getTable();
        $data = JRequest::get( 'post' );


        // Bind the form fields to the odstavce table
        if (!$row->bind($data)) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Make sure the odstavce record is valid
        if (!$row->check()) {
            $this->setError($this->_db->getErrorMsg());
            return false;
        }

        // Store the web link table to the database
        if (!$row->store()) {
            $this->setError( $row->getError() );
            return false;
        }
        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access	public
     * @return	boolean	True on success
     */
    function delete()
    {
        $id = JRequest::getVar( 'id', 0, '', 'integer' );
        $row =& $this->getTable();
        if (!$row->delete( $id )) {
            $this->setError( $row->getErrorMsg() );
            return false;
        }
        return true;
    }

}

